package de.fhd.pms.model;

import java.beans.PropertyEditorSupport;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Pattern;

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
/**
 * Basic Model Class
 */
@Entity
@Table(name = "users")
public class User implements Serializable {

	@Id
	@GeneratedValue
	@Column(name = "id")
	private Integer id;

	@Column(name = "birthdate")
	private Date birthdate;

	@NotEmpty
	@Length(min = 4, max = 7)
	@Column(name = "name")
	private String name;

	@NotEmpty
	@Length(min = 4, max = 7)
	@Column(name = "lastname")
	private String lastname;

	/*
	 * regexp taken from
	 * http://www.mkyong.com/regular-expressions/how-to-validate
	 * -password-with-regular-expression/ ( # Start of group (?=.*\d) # must
	 * contains one digit from 0-9 (?=.*[a-z]) # must contains one lowercase
	 * characters (?=.*[A-Z]) # must contains one uppercase characters
	 * (?=.*[@#$%]) # must contains one special symbols in the list "@#$%" . #
	 * match anything with previous condition checking {6,20} # length at least
	 * 6 characters and maximum of 20 ) # End of group
	 */
	@NotEmpty
	//@Pattern(regexp = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})")
	@Column(name = "password")
	private String password;

	@Transient
	private String passwordConfirm;

	@NotEmpty
	@Column(name = "email")
	@Email
	private String email;

	@NotEmpty
	@Length(min = 1, max = 50)
	@Column(name = "username", unique = true)
	private String username;

	@Range(min = 1, max = 2)
	@Column(name = "gender")
	private Integer gender;

	public Integer getEnabled() {
		return enabled;
	}

	public void setEnabled(Integer enabled) {
		this.enabled = enabled;
	}

	@Column(name = "enabled")
	private Integer enabled;

	public Date getBirthdate() {
		return birthdate;
	}

	public void setBirthdate(Date birthdate) {
		this.birthdate = birthdate;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Integer getGender() {
		return gender;
	}

	public void setGender(Integer gender) {
		this.gender = gender;
	}

	@InitBinder
	public void binder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
			public void setAsText(String value) {
				try {
					System.out.println(value);
					setValue(new SimpleDateFormat("dd/MM/yyyy").parse(value));
				} catch (ParseException e) {
					setValue(null);
				}
			}

			public String getAsText() {
				return new SimpleDateFormat("dd/MM/yyyy")
						.format((Date) getValue());
			}

		});

	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getPasswordConfirm() {
		return passwordConfirm;
	}

	public void setPasswordConfirm(String passwordConfirm) {
		this.passwordConfirm = passwordConfirm;
	}
	
	@ManyToMany(cascade = CascadeType.ALL)
	@JoinTable(
			name = "user_tour", 
			joinColumns = { @JoinColumn(name = "user_id") }, 
			inverseJoinColumns = { @JoinColumn(name = "tour_id") })
	private Set<Tour> tours = new HashSet<Tour>();

	public Set<Tour> getTours() {
		return tours;
	}

	public void setTours(Set<Tour> tours) {
		this.tours = tours;
	}


}
